package cn.enited.devtools.apiaggregator.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Accessors(chain = true)
public class MapActionResult<K, V> extends ActionResult {
    private static final long serialVersionUID = -5719589844646720448L;

    private Map<K, V> data;

    public MapActionResult() {
        this.data = new HashMap<>();
    }

    public MapActionResult(int code, String message) {
        super(code, message);
        this.data = new HashMap<>();
    }

    public MapActionResult(int code, String message, Map<K, V> data) {
        super(code, message);
        this.data = Objects.isNull(data) ? new HashMap<>() : data;
    }

    public void put(K key, V value) {
        this.data.put(key, value);
    }

    public V putIfAbsent(K key, V value) {
        return this.data.putIfAbsent(key, value);
    }

    public void putAll(Map<? extends K, ? extends V> map) {
        this.data.putAll(map);
    }

    public void clear() {
        this.data.clear();
    }

    public static <K, V> MapActionResult<K, V> of(int code, String message, Map<K, V> data) {
        return new MapActionResult<>(code, message, data);
    }
}
